<template>
  <div class="app-container">


    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="广告内容" prop="content">
        <el-input v-model="queryParams.content" placeholder="请输入广告内容" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            
            icon="Plus"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>




    <el-table border v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="100">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="广告类型" align="center" prop="type" />

      <el-table-column label="广告发布内容" align="center" prop="content" :show-overflow-tooltip="true">
        <template #default="scope">
          <span @click="showContent(scope.row)">{{ scope.row.content }}</span>
        </template>
      </el-table-column>



      <el-table-column label="投放形式" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="scope.row.isPublish" link type="primary" icon="edit"
            @click="handleUpdate(scope.row)">投放形式</el-button>
        </template>
      </el-table-column>


      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="isPublish">
        <template #default="scope">
          <span v-if="scope.row.isPublish == true">已发布</span>
          <span v-if="scope.row.isPublish == false">未发布</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="updateUserName" />





      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" v-if="!scope.row.isPublish" icon="Edit"
            @click="handlePublish(scope.row)">发布</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />



    <!--  投放形式 -->
    <el-dialog title="滚动字幕投放详情" v-model="open2" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="投放范围" prop="scope">
              <el-select v-model="form.scope" placeholder="">
                <el-option v-for="item in scopeList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投放时间" prop="timeList">
              <el-input v-model="form.timeList" placeholder="请输入时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投放位置" prop="position">
              <el-select v-model="form.position" placeholder="">
                <el-option v-for="item in arr1" :key="item.value" :label="item.title" :value="item.type">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投放日期" prop="dateList">
              <el-input :value="form.dateList.join('')" placeholder="请输入日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投放次数" prop="times">
              <el-input v-model="form.times" placeholder="请输入次数" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>


    <!-- 添加或修改图片对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="广告位置" prop="position">
              <el-select v-model="form.position" placeholder="请选择位置">
                <el-option v-for="dict in arr2" :key="dict.value" :label="dict.title" :value="dict.type"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="广告范围" prop="scope">
              <el-select v-model="form.scope" placeholder="请选择范围">
                <el-option v-for="dict in scopeList2" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="广告次数" prop="times">
              <el-input v-model="form.times" type="number" placeholder="请输入次数" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item class="" label="广告日期" prop="scope">
              <el-date-picker class="flex-1" v-model="form.dateList" value-format="YYYY-MM-DD" type="dates"
                range-separator="To" start-placeholder="Start date" end-placeholder="End date" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="广告时间" prop="scope">
              <div class="mr-6" v-for="item, index in form.timeList" :key="index">
                <el-time-picker class="flex-1" v-model="form.timeList[index]" format="HH:mm" value-format="HH:mm"
                  start-placeholder="Start time" end-placeholder="End time" />
                <el-button icon="Plus" @click="form.timeList.push('')"></el-button>
              </div>
            </el-form-item>
          </el-col>


        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>






  </div>
</template>

<script setup name="Notice">

import { ElMessageBox } from 'element-plus' 



import {
  advertisementApisupervisedetail,
} from "@/api/backAdvertisement/backAdvertisement";



import {
  advertisementRuleApirule
} from "@/api/backAdvertisementRule/backAdvertisementRule.js";



import {
  listChar,
  advertisementApiPublish,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/backAdvertisement/backAdvertisement.js";




import { onMounted } from "vue";


const { proxy } = getCurrentInstance();
const { scope_status, text_position_status } = proxy.useDict(
  "scope_status",
  "text_position_status"
);



let scopeList = [
  { label: "所在地一公里内发布", value: 1 },
  { label: "所在区县内发布", value: 2 },
  { label: "所在市内发布", value: 3 },
  { label: "所在省内发布", value: 4 },
  { label: "所在全国内发布", value: 5 },
]

let scopeList2 = [
  { label: "所在地一公里内发布", value: 1 },
  { label: "所在区县内发布", value: 2 },
  { label: "所在市内发布", value: 3 },
  { label: "所在省内发布", value: 4 },
  { label: "所在全国内发布", value: 5 },
]




let arr = []
let arr1 = [
  { title: "用户端云平台界面头条位", type: 1, checked: false },
  { title: "用户端云平台界面次条位", type: 2, checked: false },
  { title: "用户端医疗机构界面头条位", type: 3, checked: false },
  { title: "用户端医疗机构界面次条位", type: 4, checked: false },
  { title: "用户端护工抢单界面头条位", type: 5, checked: false },
  { title: "用户端护工抢单界面次条位", type: 6, checked: false },
  { title: "用户端状态查询界面头条位", type: 7, checked: false },
  { title: "用户端状态查询界面次条位", type: 8, checked: false },
  /*  */
  { title: "管理端医疗机构界面头条位", type: 9, checked: false },
  { title: "管理端医疗机构界面次条位", type: 10, checked: false },
  { title: "管理端日常经营界面头条位", type: 11, checked: false },
  { title: "管理端日常经营界面次条位", type: 12, checked: false },
  { title: "管理端生产物流界面头条位", type: 13, checked: false },
  { title: "管理端生产物流界面次条位", type: 14, checked: false },
];


let arr2 = [
  { title: "用户端云平台界面头条位", type: 1, checked: false },
  { title: "用户端云平台界面次条位", type: 2, checked: false },
  { title: "用户端医疗机构界面头条位", type: 3, checked: false },
  { title: "用户端医疗机构界面次条位", type: 4, checked: false },
  { title: "用户端护工抢单界面头条位", type: 5, checked: false },
  { title: "用户端护工抢单界面次条位", type: 6, checked: false },
  { title: "用户端状态查询界面头条位", type: 7, checked: false },
  { title: "用户端状态查询界面次条位", type: 8, checked: false },
  /*  */
  { title: "管理端医疗机构界面头条位", type: 9, checked: false },
  { title: "管理端医疗机构界面次条位", type: 10, checked: false },
  { title: "管理端日常经营界面头条位", type: 11, checked: false },
  { title: "管理端日常经营界面次条位", type: 12, checked: false },
  { title: "管理端生产物流界面头条位", type: 13, checked: false },
  { title: "管理端生产物流界面次条位", type: 14, checked: false },
];


const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


const data = reactive({
  form: {
    timeList: ['']
  },
  queryParams: {
    currentPage: 1,
    pageSize: 10,
  },
  rules: {
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询字幕列表 */
function getList() {
  loading.value = true;
  listChar(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  // form.value.times = ' '
  // form.value.timeList = ['']
  // form.value.dateList = []
  form.value = {
    times: '',
    timeList: [''],
    dateList: [],
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      advertisementApiPublish(form.value).then((response) => {
        proxy.$modal.msgSuccess("成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.advertisementId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}




/** 删除按钮操作 */
function handleDelete(row) {
  const advertisementIds = row.advertisementId || ids.value;
  proxy.$modal
    .confirm('是否确认删除字幕编号为"' + advertisementIds + '"的数据项？')
    .then(function () {
      return delNotice(advertisementIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}



/**修改按钮操作 */
let open2 = ref(false)
/* 投放 */
function handleUpdate(row) {
  reset();
  const advertisementId = row.advertisementId || ids.value;
  advertisementApisupervisedetail(advertisementId).then((response) => {
    form.value = response;
    form.value.dateList = [...form.value.dateList]
    open2.value = true;
    title.value = "投放形式";
  });
}


/* 发布操作 */
function handlePublish(row) {
  reset();
  open.value = true;
  title.value = "发布广告图片";
  form.value.advertisementId = row.advertisementId
}


/** 查看内容 */
function showContent(row) {
  ElMessageBox.alert(row.content, '广告发布内容', {
    confirmButtonText: '',
    showConfirmButton: false,
  })
}


onMounted(() => {
  getList();
  // 获取设置过的值
  advertisementRuleApirule(1).then((res) => {
    const result = res.positionList.flatMap((g) => {
      return arr2.find((i) => i.type === g) || null;
    }).filter(Boolean);
    arr2 = result
    scopeList2 = res.scopeList.map((val) => scopeList.find(i => val == i.value))
  })
})


</script>